home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Startup Screen Picker 1.2 / source / ssp INIT ƒ / ssp code ƒ / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.7 KB  |  219 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        prefs.c
  4.  
  5. Purpose:    This module handles the preferences file, which contains
  6.             the real filename of the current startup screen.
  7.             
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "prefs.h"
  26. #include "init.h"
  27. #include "environment.h"
  28. #include "main.h"
  29.  
  30. static PrefStruct    thePrefs;
  31. static long            gPrefsFilePos;
  32.  
  33. short PreferencesInit(void)
  34. {
  35.     short            prefsFileID;
  36.     short            err;
  37.     
  38.     err=OpenPrefsFile(&prefsFileID);
  39.     if (err!=prefs_allsWell)
  40.     {
  41.         if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
  42.             ClosePrefsFile(prefsFileID);
  43.         return err;
  44.     }
  45.     
  46.     GetNextPrefs(prefsFileID);
  47.     if (err!=prefs_allsWell)
  48.     {
  49.         ClosePrefsFile(prefsFileID);
  50.         return err;
  51.     }
  52.     
  53.     CopyPrefsToGlobals();
  54.     ClosePrefsFile(prefsFileID);
  55.     
  56.     return prefs_allsWell;
  57. }
  58.  
  59. short OpenPrefsFile(short *prefsFileID)
  60. {
  61.     short            thisFile;
  62.     OSErr            isHuman;
  63.     short            vRefNum;
  64.     long            dirID;
  65.     FSSpec            prefsFile;
  66.     FInfo            prefsInfo;
  67.     short            temp;
  68.     long            count;
  69.     Boolean            newPrefs;
  70.     short            err;
  71.     unsigned char    *name=PREFS_FILE_NAME;
  72.     
  73.     newPrefs=FALSE;
  74.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  75.     
  76.     if (isHuman!=noErr)
  77.         return prefs_cantOpenPrefsErr;
  78.  
  79.     if (gHasFSSpecs)
  80.     {
  81.         isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
  82.         if (isHuman!=noErr)
  83.         {
  84.             if (isHuman==fnfErr)
  85.             {
  86.                 isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
  87.                 if (isHuman!=noErr)
  88.                     return prefs_cantCreatePrefsErr;
  89.                 newPrefs=TRUE;
  90.             }
  91.             else return prefs_cantOpenPrefsErr;
  92.         }
  93.         isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
  94.         *prefsFileID=thisFile;
  95.         if (isHuman!=noErr)
  96.             return prefs_cantOpenPrefsErr;
  97.     }
  98.     else
  99.     {
  100.         isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  101.         *prefsFileID=thisFile;
  102.         if (isHuman!=noErr)
  103.         {
  104.             if (isHuman==fnfErr)
  105.             {
  106.                 isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
  107.                 if (isHuman!=noErr)
  108.                     return prefs_cantCreatePrefsErr;
  109.                 prefsInfo.fdType=PREFS_TYPE;
  110.                 prefsInfo.fdCreator=CREATOR;
  111.                 prefsInfo.fdFlags=0;
  112.                 prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
  113.                 prefsInfo.fdFldr=0;
  114.                 isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
  115.                 if (isHuman!=noErr)
  116.                     return prefs_cantCreatePrefsErr;
  117.                 isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  118.                 *prefsFileID=thisFile;
  119.                 if (isHuman!=noErr)
  120.                     return prefs_cantOpenPrefsErr;
  121.                 newPrefs=TRUE;
  122.             }
  123.             else return prefs_cantOpenPrefsErr;
  124.         }
  125.     }
  126.     if (newPrefs)
  127.     {
  128.         return Virgin(*prefsFileID);
  129.     }
  130.     
  131.     return prefs_allsWell;
  132. }
  133.  
  134. void ClosePrefsFile(short prefsFileID)
  135. {
  136.     FSClose(prefsFileID);
  137.     FlushVol(0L, kOnSystemDisk);
  138. }
  139.  
  140. short GetNextPrefs(short prefsFileID)
  141. {
  142.     OSErr        isHuman;
  143.     long        count;
  144.     
  145.     count=sizeof(thePrefs);
  146.     isHuman=FSRead(prefsFileID, &count, &thePrefs);
  147.     if (isHuman!=noErr)
  148.         return prefs_diskReadErr;
  149.     
  150.     return prefs_allsWell;
  151. }
  152.  
  153. short SavePrefs(short prefsFileID)
  154. {
  155.     long        oldEOF;
  156.     OSErr        isHuman;
  157.     long        count;
  158.     
  159.     isHuman=SetEOF(prefsFileID, sizeof(thePrefs));
  160.     if (isHuman!=noErr)
  161.         return prefs_diskWriteErr;
  162.  
  163.     SetFPos(prefsFileID, 1, 0L);
  164.     count=sizeof(thePrefs);
  165.     isHuman=FSWrite(prefsFileID, &count, &thePrefs);
  166.     if (isHuman!=noErr)
  167.         return prefs_diskWriteErr;
  168.     
  169.     return prefs_allsWell;
  170. }
  171.  
  172. short Virgin(short prefsFileID)
  173. {
  174.     short            err;
  175.     
  176.     DefaultPrefs();
  177.     CopyGlobalsToPrefs();
  178.     err=SavePrefs(prefsFileID);
  179.     
  180.     return (err==prefs_allsWell) ? prefs_virginErr : err;
  181. }
  182.  
  183. void DefaultPrefs(void)
  184. {
  185.     unsigned char    *temp="\pOld StartupScreen";
  186.     short            i;
  187.     
  188.     for (i=temp[0]; i>=0; i--)
  189.         gLastName[i]=temp[i];
  190. }
  191.  
  192. void CopyGlobalsToPrefs(void)
  193. {
  194.     short            i;
  195.     
  196.     for (i=0; i<32; i++)
  197.         thePrefs.name[i]=0x00;
  198.     for (i=gLastName[0]; i>=0; i--)
  199.         thePrefs.name[i]=gLastName[i];
  200. }
  201.  
  202. void CopyPrefsToGlobals(void)
  203. {
  204.     short            i;
  205.     
  206.     for (i=thePrefs.name[0]; i>=0; i--)
  207.         gLastName[i]=thePrefs.name[i];
  208. }
  209.  
  210. void SaveThePrefs(void)
  211. {
  212.     short            prefsFileID;
  213.     
  214.     OpenPrefsFile(&prefsFileID);
  215.     CopyGlobalsToPrefs();
  216.     SavePrefs(prefsFileID);
  217.     ClosePrefsFile(prefsFileID);
  218. }
  219.